#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;

#endregion

namespace Library
{
    public partial class FormBookShelf : Form
    {
        BookShelf m_BookShelf;
        User m_currnUser;
        BookShelfService m_BookShelfService = new BookShelfService();
        UserControlBookShelf m_UserControlBookShelf;
        bool m_apply = false;

        public FormBookShelf(BookShelf BookShelf, User currnBookShelf, UserControlBookShelf BookShelfControlBookShelf)
        {
            InitializeComponent();
            labelTitle.Text = "Book Shelf";
            Text = "Book Shelf";
            m_BookShelf = BookShelf;
            m_currnUser = currnBookShelf;
            m_UserControlBookShelf = BookShelfControlBookShelf;
            loadBookShelf();
            initEnterKeyDown(this.Controls);
        }

        private void initEnterKeyDown(Control.ControlCollection controls)
        {
            foreach (Control c in controls)
            {
                if (c is TextBox)
                {
                    TextBox txtB = (TextBox)c;
                    txtB.EnterKeyDown += new KeyEventHandler(txtB_EnterKeyDown);
                }
                if (c is CheckBox)
                {
                    CheckBox txtB = (CheckBox)c;
                    txtB.KeyDown += new KeyEventHandler(txtB_KeyDown);
                }
                initEnterKeyDown(c.Controls);
            }
        }

        void txtB_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                Control ctrl = (Control)objSender;
                SelectNextControl(ctrl, true, true, true, true);
            }
        }

        void txtB_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            Control ctrl = (Control)objSender;
            SelectNextControl(ctrl, true, true, true, true);
        }

        private void loadBookShelf()
        {
            textBoxCode.Text = m_BookShelf.Code;
            textBoxName.Text = m_BookShelf.Name;
            textBoxRemark.Text = m_BookShelf.Remark;
            textBoxCode.Enabled = m_BookShelf.Id == 0;
            buttonApply.Visible = m_BookShelf.Id != 0;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (isValid())
            {
                UpdateEntity();
                if (m_BookShelf.Id == 0)
                {
                    m_BookShelfService.InsertNewBookShelf(m_BookShelf);
                    m_UserControlBookShelf.AddNewData(m_BookShelf);
                }
                else
                {
                    m_BookShelfService.UpdateBookShelf(m_BookShelf);
                    m_UserControlBookShelf.UpdateData(m_BookShelf);
                }
                MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, new EventHandler(CloseForm));
               
            }
        }
        void CloseForm(object sender, EventArgs e)
        {
            if (!m_apply)
                Close();
            m_apply = false;
        }

        private void UpdateEntity()
        {
            m_BookShelf.Code = textBoxCode.Text;
            m_BookShelf.Name = textBoxName.Text;
            m_BookShelf.Remark = textBoxRemark.Text;
            m_BookShelf.CreatedUserId = m_currnUser.Id;
            m_BookShelf.ModifiedUserId = m_currnUser.Id;
        }

        private bool isValid()
        {
            clearError();
            bool validcode = textBoxCode.Text != "";
            bool validname = textBoxName.Text != "";
            bool validexist = true;
            if (m_BookShelf.Id == 0)
                validexist = m_BookShelfService.GetBookShelfByBookShelfCode(textBoxCode.Text.Trim()) == null;
            if (!validcode) errorProvider.SetError(textBoxCode, "Fill Code");
            if (!validname) errorProvider.SetError(textBoxName, "Fill Name");
            if (!validexist) errorProvider.SetError(textBoxCode, "Code already exist");
            return validcode && validname && validexist;
        }

        private void clearError()
        {
            errorProvider.SetError(textBoxCode, "");
            errorProvider.SetError(textBoxName, "");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            m_apply = true;
            buttonSave_Click(sender, e);
        }

    }
}